package com.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Map;

/**
 * Created by aszer on 2016/11/12.
 */
public class DAOContainer {
    /*    public static Object getT(Hib dao, String[] args) {
            try {
                Class<?> clazz = Class.forName("com.hdb."+dao.getTableName());
                Object bean = clazz.newInstance();
                Field[] fileds = clazz.getDeclaredFields();
                int i=0;
                for (Field f : fileds) {
                    //基于顺序注入
                    Class<?> c = f.getType();
                    //允许访问private字段
                    f.setAccessible(true);
                    //注入属性
                    inject(bean,f,args[i]);
                    i++;
                }
                return bean;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }*/
    public static Object updateT(Object bean, Class<?> type, Map<String, String> args) {
        try {
            Field[] fileds = type.getDeclaredFields();
            for (Field f : fileds) {
                //基于变量名注入
                String name = f.getName();
                //允许访问private字段
                f.setAccessible(true);
                //注入属性
                if (args.containsKey(name)) {
                    inject(bean, f, args.get(name));
                }
            }
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void getter(Object obj, String att) {
        try {
            Method method = obj.getClass().getMethod("get" + att);
            System.out.println(method.invoke(obj));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setter(Object obj, String att, Object value,
                              Class<?> type) {
        try {
            Method method = obj.getClass().getMethod("set" + att, type);
            method.invoke(obj, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*    public static Object getTByName(Hib dao,Map<String,String> args) {
            try {
                Class<?> clazz = Class.forName("com.hdb."+dao.getTableName());
                Object bean = clazz.newInstance();
                Field[] fileds = clazz.getDeclaredFields();
                for (Field f : fileds) {
                    //基于顺序注入
                    String name=f.getName();

                    //允许访问private字段
                    f.setAccessible(true);
                    //注入属性
                    if(args.containsKey(name)){
                        inject(bean,f,args.get(name));
                    }
                }
                return bean;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }*/
    private static void inject(Object bean, Field f, String arg) throws IllegalAccessException {
        Class<?> c = f.getType();
        if (c.equals(int.class)) {
            f.set(bean, Integer.parseInt(arg));
        } else if (c.equals(Timestamp.class)) {
            f.set(bean, new Timestamp(Integer.parseInt(arg)));
        } else {
            f.set(bean, arg);
        }
    }
}